<template>
  <el-card style="padding: 10px;margin-bottom: 10px ;">
    <el-form label-width="120px" inline>
      <el-col :span="8">
        <el-form-item label="一级分类">
          <el-select v-model="CategoryStore.category1Id" placeholder="请选择一级分类" style="display:"
            :disabled="!CategoryStore.Addshow">
            <!-- 
            label option显示的文字
            value option选中得到的值
           -->
            <el-option :label="item.name" :value="item.id" v-for="(item) in CategoryStore.category1List"
              :key="item.id" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="二级分类">
          <el-select v-model="CategoryStore.category2Id" placeholder="请选择二级分类" :disabled="!CategoryStore.Addshow">
            <el-option :label="item.name" :value="item.id" v-for="(item) in CategoryStore.category2List"
              :key="item.id" />
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="8">
        <el-form-item label="三级分类">
          <el-select v-model="CategoryStore.category3Id" :placeholder="'请选择三级分类'" :disabled=" !CategoryStore.Addshow">
            <el-option :label="item.name" :value="item.id" v-for="(item) in CategoryStore.category3List"
              :key="item.id" />
          </el-select>
        </el-form-item>
      </el-col>
    </el-form>
  </el-card>


</template>

<script lang="ts">
export default {
  name: "CategorySelectorStore",
};
</script>


<!-- watch(() => form.oneClassificationValue, async (newVal) => {
  let result = await getTowClassificationAttr(+newVal)
  twoClassification.value = result
  form.twoClassificationValue = ''
  form.threeClassificationValue = ''
  threeClassification.value = []

})
watch(() => form.twoClassificationValue, async (newVal) => {
  let result = await getThreeClassificationAttr(+newVal)
  threeClassification.value = result
  form.threeClassificationValue = ''
})

watch(() => form.threeClassificationValue, async (newVal) => {
  console.log(newVal);

  // const result = await getAttributesAttr(Number(form.oneClassificationValue), Number(form.twoClassificationValue), Number(newVal))
  // console.log(result);


}) -->
<script lang="ts" setup>
import { useCategoryStore } from '@/stores/category'
import { reactive, watch, onMounted } from 'vue'
const CategoryStore = useCategoryStore()


/**
 * 1.一上来请求一级分类数据进项展示
 * 2.选择一级分类,请求二级分裂展示
 * 3.选择二级分裂,请求三级分类展示
 * 
 * 
 * 问题:
 *     当重新选择一级分类,二级分裂选中的值和数据都要清空  三级分类选中的值和数据也都要清空
 *     当重新选择二级分类,三级分类选中的值和数据都要清空
 * */
onMounted(async () => {
  // 1. 一上来请求一级分类数据进行展示
  CategoryStore.getCategory1List();
});

// 监视一级分类id
// 当一级分类id发送变化，请求二级分类展示
watch(
  () => CategoryStore.category1Id,
  async (newVal) => {
    CategoryStore.getCategory2List(newVal as number);
  }
);

// 监视二级分类id
// 当二级分类id发送变化，请求三级分类展示
watch(
  () => CategoryStore.category2Id,
  async (newVal) => {
    if (!newVal) return;
    CategoryStore.getCategory3List(newVal as number);
  }
);

</script>





<style scoped>

</style>